<?php
/**
 * 使用示例
 * @author next
 * @version 1.0
 * create Date 2013-11-4
 * update Date 2013-11-5
 * update Date 2013-11-6
 * <yl:related><yl:content type="title"/></yl:related>
 */
class YlRelatedTaglib {
    
    private $_dbPrefix = 'yl_';
    
    public function __construct() {
        C(include SITE_PATH.'/extends/taglib/tagarr.php');
        $dbPrefix = C('db_prefix');
        !empty($dbPrefix) && $this->_dbPrefix = $dbPrefix;
    }
    public function parse($content,$sid,$cateid,$contentid,$contentrelated,$domain,$channel) {
        if (stripos($content, 'yl:related') !== false) {
            $matches = preg_parse($content, 'yl:related');
            foreach ($matches as $match) {
                $parseStr = '';
            	$contentText = getParseContentText($match, 'yl:related');         
                $artnamingsql='"'.$channel['artnaming'].'"';
                if ($category_list = S('cache_channel_'.$cateid) === false) {
                   $category_list = M('cms_category')->where('id='.$cateid)->find();
                   S('cache_channel_'.$cateid, $category_list);
                }
                if(!empty($category_list['artnaming'])){
                   $artnamingsql='"'.$category_list['artnaming'].'"';
                }
                if(!empty($contentrelated)) {
                	$contentrelatedarr = explode(",",$contentrelated);
                	foreach ( $contentrelatedarr as $kk => $vcontentrelated ) {
                		if (($v = S('cache_contents_'.$vcontentrelated)) === false) {
                			$v = M()->query('select (case when url <> "" then url else
    (concat("'.$domain.'","/",replace(replace('.$artnamingsql.',
    "{cateid}",cid),"{contentid}",id))) end) as url,
     replace(imageurl,"@","'.$domain.'") as imageurl,replace(big_imageurl,"@","'.$domain.'") as big_imageurl,title,
     subtitle,author,updatetime,inputtime,tags,
     source,content,readpoint,keywords,cid,id,siteid,description,create_uid,create_uname,update_uid,summary,group_name_collection,
     update_uname,fileurl,@row := @row + 1 as ItemIndex from '.$this->_dbPrefix.'cms_article,(select @row := 0) r where id='.$vcontentrelated);
                			S('cache_contents_'.$vcontentrelated, $v);
                		}
                        $text = taglib(C('yl:if'))->parse($contentText, $v);
                        $text = taglib(C('yl:a'))->parse($text,$sid,$v,'1',$domain,$channel);
                        $text = taglib(C('yl:content'))->parse($text,$v,$domain);
                        $text = taglib(C('yl:image'))->parse($text,$v);
                        $text = taglib(C('yl:count'))->parse($text,$v);

                        if(strpos($text,'{') && strpos($text,'}')) {
                            preg_match_all('/\{Content.(.*?)\}/eis',$text,$out);
                            for ($i=0; $i< count($out[1]); $i++) {
                                $text = str_ireplace($out[0][$i],$v[$out[1][$i]],$text);
                            }
                        }
                        $parseStr .= $text;
                    }
                } else {
                    $parseStr = '';
                }
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
    }
}